﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace SC.CodeBuilder
{
    partial class Builder
    {
        #region 生成IDAL
        /// <summary>
        /// 生成BLL
        /// </summary>
        /// <returns></returns>
        public StringBuilder BuildeIDALSB()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("using System;").AppendLine();
            sb.Append("using System.Data;").AppendLine();
            sb.Append("using System.Collections.Generic;").AppendLine();
            sb.Append("using System.Data.SqlClient;").AppendLine();
            sb.Append("namespace ").Append(unameSpace).Append("IDAL").AppendLine();
            sb.Append("{").AppendLine();
            sb.Append("    public partial interface I").Append(tableName).AppendLine();
            sb.Append("    {");
            sb.AppendLine();

            if (Add) { BuilderIDALAdd(sb); }
            if (Delete) { BuilderIDALDelete(sb); }
            if (Update) { BuilderIDALUpdate(sb); }
            if (GetModel) { BuilderIDALGetModel(sb); }
            if (GetList) { BuilderIDALGetList(sb); }
            if (Exists) { BuilderIDALExists(sb); }
            if (GetMaxID) { BuilderIDALGetMaxId(sb); }
            if (GetRecordCount) { BuilderIDALGetRecordCount(sb); }

            sb.Append("    }").AppendLine();
            sb.Append("}").AppendLine();
            return sb;
        }
        #endregion

        #region 增加一条数据 - void BuilderIDALAdd(StringBuilder sb)
        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderIDALAdd(StringBuilder sb)
        {
            sb.Append("        #region 增加一条数据").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 增加一条数据").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            if (hasidentity)
            {
                sb.Append("        ").Append("Model.").Append(tableName).Append(" Add(").Append(unameSpace).Append("Model.").Append(tableName).AppendLine(" model);");
            }
            else
            {
                sb.Append("        int Add(").Append(unameSpace).Append("Model.").Append(tableName).AppendLine(" model);");
            }
            sb.Append("        #endregion").AppendLine();
            sb.AppendLine();
        }
        #endregion

        #region 更新一条数据 - void BuilderIDALUpdate(StringBuilder sb)
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderIDALUpdate(StringBuilder sb)
        {
            sb.Append("        #region 更新一条数据").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 更新一条数据").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        bool Update(").Append(unameSpace).Append("Model.").Append(tableName).AppendLine(" model);");
            sb.Append("        #endregion");
            sb.AppendLine();
        }
        #endregion

        #region 删除一条数据 - void BuilderIDALDelete(StringBuilder sb)
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderIDALDelete(StringBuilder sb)
        {
            sb.Append("        #region 删除一条数据").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 删除一条数据").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            if (hasidentity)//自增&主键
            {
                sb.Append("        bool Delete(").Append(identityType).Append(" ").Append(identityName).AppendLine(");//自增&主键");
            }
            else//无主键
            {
                sb.Append("        bool Delete(Model.").Append(tableName).AppendLine(" model);//自增主键 全无");
            }
            sb.Append("        #endregion").AppendLine();
            sb.AppendLine();
        }
        #endregion

        #region 得到一个对象实体 - void BuilderIDALGetModel(StringBuilder sb)
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderIDALGetModel(StringBuilder sb)
        {
            #region 得到一个对象实体
            sb.Append("        #region 得到一个对象实体").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        ///  得到一个对象实体").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            if (hasidentity)//自增&主键
            {
                sb.Append("        ").Append(unameSpace).Append("Model.").Append(tableName).Append(" GetModel(").Append(identityType).Append(" ").Append(identityName).AppendLine(");//自增&主键");
            }
            else //无主键
            {
                sb.Append("        ").Append(unameSpace).Append("Model.").Append(tableName).AppendLine(" GetModel(");
                JoinDrWithType(sb,false);
                sb.Append(");//自增主键 全无").AppendLine();
            }
            sb.Append("        #endregion").AppendLine();
            sb.AppendLine();
            #endregion
        }
        #endregion

        #region 获得数据列表 - void BuilderIDALGetList(StringBuilder sb)
        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderIDALGetList(StringBuilder sb)
        {
            sb.Append("        #region 获得数据列表").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 获得数据列表").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        List<Model.").Append(tableName).AppendLine("> GetModels();");
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 获得数据列表").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        /// <param name=\"strWhere\">Where条件</param>").AppendLine();
            sb.Append("        /// <returns></returns>").AppendLine();
            sb.Append("        List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, params SqlParameter[] parameters);");
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 获得数据列表").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        /// <param name=\"top\">Top 条件</param>").AppendLine();
            sb.Append("        /// <param name=\"strWhere\">Where 条件</param>").AppendLine();
            sb.Append("        /// <param name=\"order\">Order 条件</param>").AppendLine();
            sb.Append("        /// <param name=\"parameters\">参数</param>").AppendLine();
            sb.Append("        /// <returns></returns>").AppendLine();
            sb.Append("        List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, int top, string order, params SqlParameter[] parameters);");
            sb.Append("        #endregion").AppendLine();
            sb.Append("        #region 分页获取数据列表").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 分页获取数据列表").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, string orderby, int pageIndex, int pageSize);");
            sb.Append("        #endregion").AppendLine();
            sb.AppendLine();
            sb.AppendLine();
        }
        #endregion

        #region 是否存在该记录 - void BuilderIDALExists(StringBuilder sb)
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderIDALExists(StringBuilder sb)
        {
            sb.Append("        #region 是否存在该记录").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 是否存在该记录").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            if (hasidentity)//自增&主键
            {
                sb.Append("        bool Exists(").Append(identityType).Append(" ").Append(identityName).AppendLine(");//自增&主键");
            }
            else//无主键
            {
                sb.Append("        bool Exists(Model.").Append(tableName).AppendLine(" model);//自增主键 全无");
            }
            sb.Append("        #endregion").AppendLine();
            sb.AppendLine();
        }
        #endregion

        #region 获取记录总数 - static void BuilderIDALGetRecordCount(StringBuilder sb)
        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderIDALGetRecordCount(StringBuilder sb)
        {
            sb.Append("        #region 获取记录总数").AppendLine();
            sb.Append("        /// <summary>").AppendLine();
            sb.Append("        /// 获取记录总数").AppendLine();
            sb.Append("        /// </summary>").AppendLine();
            sb.Append("        int GetRecordCount();").AppendLine();
            sb.Append("        int GetRecordCount(string strWhere);").AppendLine();
            sb.Append("        #endregion").AppendLine();
            sb.AppendLine();
        }
        #endregion

        #region 得到最大的ID - void BuilderIDALGetMaxId(StringBuilder sb)
        /// <summary>
        /// 得到最大的ID
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderIDALGetMaxId(StringBuilder sb)
        {
            if (hasidentity)//自增&主键
            {
                //sb.Append("        #region 得到最大的ID").AppendLine();
                //sb.Append("        int GetMaxID();").AppendLine();
                //sb.Append("        #endregion").AppendLine();
                //sb.AppendLine();
            }
        }
        #endregion
    }
}
